package com.wondersgroup.fjzlpt.org.dao.impl.qytj;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.SqlBeanBaseJTDaoImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.org.dao.inter.Qytj.QytjDAO;
import com.wondersgroup.fjzlpt.org.util.ControllerCommonUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 签约统计实现层
 * <p>
 * <p>
 * Created by sanma on 2017/10/11.
 */
@Repository
public class QytjDAOImpl extends SqlBeanBaseJTDaoImpl implements QytjDAO {
    @Override
    public JSONObject queryTjxxPage(Map map) {
        ControllerCommonUtil.pageFormat(map);
        JSONObject sqlObj = getQuerySqlOnView(map);
        String sql = sqlObj.getString("sql");
        List list = sqlObj.getJSONArray("sqlParams");
        PageQuerySQLBean sb = checkAndCreatePageQuerySQLBean(map, sql, null, list.toArray());
        JSONObject jsonObject = super.queryPage(sb,
                ResultHandlerConstant.JSONARRAY);
        return jsonObject;
    }

    @Override
    public JSONArray queryQysjxx(Map map) {
        StringBuffer sb = new StringBuffer("SELECT * FROM V_JTQY_QYXX WHERE QYXXID IN (\n" +
                " SELECT QYXXID FROM TB_JTQY_QYSJB WHERE 1 = 1  \n");
        return queryQysjxxExecute(map , sb );
    }

    @Override
    public JSONArray queryQyZjsjxx(Map map) {
        StringBuffer sb = new StringBuffer("SELECT * FROM V_JTQY_ZJSQ WHERE ZJSQID IN ( \n" );
        sb.append(" SELECT SJGLID FROM TB_JTQY_QYSJB WHERE 1 = 1  \n");
        return queryQysjxxExecute(map , sb );
    }

    @Override
    public JSONArray queryQyBgsjxx(Map map) {
        StringBuffer sb = new StringBuffer("SELECT * FROM V_JTQY_BGFWBXX WHERE BGID IN ( \n");
        sb.append(" SELECT SJGLID FROM TB_JTQY_QYSJB WHERE 1 = 1  \n");
        return queryQysjxxExecute(map , sb );
    }

    public JSONArray queryQysjxxExecute(Map<String,String> map , StringBuffer sb ){
        sb.append(" AND PTJGID = ? AND TDID = ? AND YSID = ? AND SJLX = ? ) ");
        List<Object> list = new ArrayList<>();
        list.add(map.get("PTJGID"));
        list.add(map.get("TDID"));
        list.add(map.get("YSID"));
        list.add(map.get("SJLX"));
        SQLBean sqlBean = new SQLBean();
        sqlBean.setParamAry(list);
        sqlBean.setSql(sb.toString());
        //JSONArray ary = null;
        //if(!StringUtils.isEmpty(ids)){
        //    sb.append(" AND " + condition + " IN ( ");
        //    String[] idAry = ids.split(",");
        //    for(String id : idAry){
        //        sb.append("? ,");
        //        list.add(id);
        //    }
        //    sb.deleteCharAt(sb.length() - 1);
        //    sb.append(") ");
        //    ary = super.queryList(sqlBean);
        //}
        return super.queryList(sqlBean);
    }


    /**
     * 合成查询视图的Sql
     *
     * @return
     */
    private JSONObject getQuerySqlOnView(Map<String, String> map) {
        JSONObject result = new JSONObject();
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append("select * from V_JTQY_YSQYTJ a where 1=1 \n");

        List sqlParams = new ArrayList();
        // 居民姓名
        String value = map.get("XM");
        if (!StringUtils.isEmpty(value)) {
            sqlBuf.append("and a.YSXM like ? ");
            sqlParams.add("%" + value + "%");
        }
        // 团队名称
        value = map.get("QYTD");
        if (!StringUtils.isEmpty(value)) {
            sqlBuf.append("and a.TDMC like ? ");
            sqlParams.add("%" + value + "%");
        }
        // 医疗机构ID
        value = map.get("PTJGID");
        if (!StringUtils.isEmpty(value)) {
            sqlBuf.append(" and a.PTJGID = ? ");
            sqlParams.add(value);
        }
        // 医疗机构名称(所在医院)
        value = map.get("YLJGMC");
        if (!StringUtils.isEmpty(value)) {
            sqlBuf.append(" and a.YLJGMC like ? ");
            sqlParams.add("%" + value + "%");
        }
        // 服务包ID
        value = map.get("XZQHDM");
        if (!StringUtils.isEmpty(value)) {
            sqlBuf.append("and a.XZQHDM like ? ");
            sqlParams.add("%" + value + "%");
        }
        result.put("sql", sqlBuf.toString());
        result.put("sqlParams", sqlParams);
        return result;
    }
}
